package com.lonwin.inspectionmanagement.mvp.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.lonwin.inspectionmanagement.R;
import com.lonwin.inspectionmanagement.R2;
import com.lonwin.inspectionmanagement.app.AppConstants;
import com.lonwin.inspectionmanagement.app.utils.AppUtil;
import com.lonwin.inspectionmanagement.app.utils.DataUtil;
import com.lonwin.inspectionmanagement.app.utils.TimeUtils;
import com.lonwin.inspectionmanagement.di.component.DaggerLookPatrolRecordComponent;
import com.lonwin.inspectionmanagement.mvp.contract.LookPatrolRecordContract;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.XcjlbInfoBean;
import com.lonwin.inspectionmanagement.mvp.presenter.LookPatrolRecordPresenter;
import com.lonwin.inspectionmanagement.widget.dialog.LoadingDailog;
import com.lonwin.inspectionmanagement.widget.signature.config.PenConfig;
import com.lonwin.inspectionmanagement.widget.signature.ui.PaintActivity;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 12/25/2018 00:19
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class LookPatrolRecordActivity extends BaseActivity<LookPatrolRecordPresenter> implements LookPatrolRecordContract.View {

    @BindView(R2.id.back_icon)
    ImageView backIcon;
    @BindView(R2.id.toolbar_back)
    RelativeLayout toolbarBack;
    @BindView(R2.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R2.id.back_right_icon)
    TextView backRightIcon;
    @BindView(R2.id.toolbar_right)
    RelativeLayout toolbarRight;
    @BindView(R2.id.toolbar)
    Toolbar toolbar;
    @BindView(R2.id.patrolRecord_bt)
    TextView patrolRecordBt;
    @BindView(R2.id.patrolRecord_hhkq)
    TextView patrolRecordHhkq;
    @BindView(R2.id.patrolRecord_hz)
    TextView patrolRecordHz;
    @BindView(R2.id.patrolRecord_hhkqjbqk)
    TextView patrolRecordHhkqjbqk;
    @BindView(R2.id.patrolRecord_xcsj)
    TextView patrolRecordXcsj;
    @BindView(R2.id.patrolRecord_xcry)
    TextView patrolRecordXcry;
    @BindView(R2.id.patrolRecord_tq)
    TextView patrolRecordTq;
    @BindView(R2.id.patrolRecord_cydw)
    TextView patrolRecordCydw;
    @BindView(R2.id.patrolRecord_xcqy)
    TextView patrolRecordXcqy;
    @BindView(R2.id.patrolRecord_xcnr)
    TextView patrolRecordXcnr;
    @BindView(R2.id.patrolRecord_czwt)
    TextView patrolRecordCzwt;
    @BindView(R2.id.patrolRecord_hzclyj)
    TextView patrolRecordHzclyj;
    @BindView(R2.id.patrolRecord_qpimg)
    ImageView patrolRecordQpimg;
    @BindView(R2.id.patrolRecord_lay_qpimg)
    LinearLayout patrolRecordLayQpimg;
    @BindView(R2.id.patrolRecord_bottom_title)
    TextView patrolRecordBottomTitle;
    @BindView(R2.id.patrolRecord_save)
    Button patrolRecordSave;
    @BindView(R2.id.patrolRecord_send)
    Button patrolRecordSend;
    @BindView(R2.id.patrolRecord_bottom_layout)
    LinearLayout patrolRecord_bottom_layout;
    @BindView(R2.id.patrolRecord_hzqp_btn)
    Button patrolRecord_hzqp_btn;
    @BindView(R2.id.patrolRecord_bottomqp_layout)
    LinearLayout patrolRecord_bottomqp_layout;

    @BindView(R2.id.patrolRecord_last_line)
    View patrolRecord_last_line;

    private LoadingDailog loadingDailog;
    private Type model;
    private XcjlbInfoBean.DataBean mdataBean;
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerLookPatrolRecordComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public Activity getMyActivity() {
        return this;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (getIntent().hasExtra(AppConstants.EXTRA_STRING)) {
            toolbarTitle.setText(getIntent().getStringExtra(AppConstants.EXTRA_STRING));
        }
        mPresenter.getXcjlbById(getIntent().getStringExtra(AppConstants.EXTRA_XCJLBID));

    }

    @Override
    public void getXcjlbByIdSuccess(XcjlbInfoBean.DataBean dataBean) {
        if (dataBean == null) return;
        mdataBean=dataBean;
        patrolRecordBt.setText(dataBean.getTitle() != null ? dataBean.getTitle() : "");
        patrolRecordHhkq.setText(dataBean.getGroupnm() != null ? dataBean.getGroupnm() : "");
        patrolRecordHz.setText(dataBean.getHznm() != null ? dataBean.getHznm() : "");
        patrolRecordHhkqjbqk.setText(dataBean.getJbqk() != null ? dataBean.getJbqk() : "");
        patrolRecordXcsj.setText(TimeUtils.getNowDayAndMonth(dataBean.getXcsj()));
        patrolRecordXcry.setText(dataBean.getXcry() != null ? dataBean.getXcry() : "");
        patrolRecordTq.setText(dataBean.getTq() != null ? dataBean.getTq() : "");
        patrolRecordCydw.setText(dataBean.getCydw() != null ? dataBean.getCydw() : "");
        patrolRecordXcqy.setText(dataBean.getXcqy() != null ? dataBean.getXcqy() : "");
        patrolRecordXcnr.setText(dataBean.getXcnr() != null ? dataBean.getXcnr() : "");
        patrolRecordCzwt.setText(dataBean.getCzwt() != null ? dataBean.getCzwt() : "");
        patrolRecordHzclyj.setText(dataBean.getHzclyj() != null ? dataBean.getHzclyj() : "");
        if (dataBean.getHzclqm() != null && !TextUtils.isEmpty(dataBean.getHzclqm())) {
            patrolRecordLayQpimg.setVisibility(View.VISIBLE);
            Glide.with(this).load(DataUtil.getFileUrl(mPresenter.getConfigBean()) + "/" + dataBean.getHzclqm()).into(patrolRecordQpimg);

        } else {
            patrolRecordLayQpimg.setVisibility(View.GONE);
            patrolRecord_last_line.setVisibility(View.GONE);
        }
        model= (LookPatrolRecordActivity.Type) getIntent().getSerializableExtra(AppConstants.EXTRA_INT);
       switch (model)
       {
           case EDIT:
               patrolRecord_bottom_layout.setVisibility(View.GONE);
               backRightIcon.setVisibility(View.VISIBLE);
               backRightIcon.setText("编辑");
               backRightIcon.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       Intent intent=new Intent(LookPatrolRecordActivity.this,EditPatrolRecordActivity.class);
                       intent.putExtra(AppConstants.EXTRA_XCJLBID,dataBean.getXcjlbid());
                       startActivity(intent);
                   }
               });
           break;
           case LOOK:
               patrolRecord_bottom_layout.setVisibility(View.GONE);
               backRightIcon.setVisibility(View.GONE);
               patrolRecord_bottomqp_layout.setVisibility(View.GONE);
               break;
           case BSHZQP:
               patrolRecord_bottom_layout.setVisibility(View.VISIBLE);
               backRightIcon.setVisibility(View.GONE);
               patrolRecordBottomTitle.setText("确定报送河长处签批？");
               patrolRecordSave.setText("取消");
               patrolRecordSend.setText("报送");
               patrolRecordSave.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       killMyself();
                   }
               });
               patrolRecordSend.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                     //联络员报送河长签批
                       Map<String,String> maps=new ArrayMap<>();
                       maps.put("gpsid",String.valueOf(mPresenter.getConfigBean().getGpsId()));
                       maps.put("gpsnm",mPresenter.getConfigBean().getGpsName());
                       maps.put("xcjlbid",mdataBean.getXcjlbid());
                       mPresenter.submittedXcjlb("submittedXcjlb",maps);
                   }
               });
               break;
           case  FSHZBCD:
               //联络员发送给河长办存档
               patrolRecord_bottom_layout.setVisibility(View.VISIBLE);
               backRightIcon.setVisibility(View.GONE);
               patrolRecordBottomTitle.setText("确定发送河长办存档？");
               patrolRecordSave.setText("取消");
               patrolRecordSend.setText("发送");
               patrolRecordSave.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       killMyself();
                   }
               });
               patrolRecordSend.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       //联络员报送河长办存档
                       Map<String,String> maps=new ArrayMap<>();
                       maps.put("gpsid",String.valueOf(mPresenter.getConfigBean().getGpsId()));
                       maps.put("gpsnm",mPresenter.getConfigBean().getGpsName());
                       maps.put("xcjlbid",mdataBean.getXcjlbid());
                       mPresenter.submittedXcjlb("neatenXcjlb",maps);
                   }
               });
               break;
           case HZQP:
               backRightIcon.setVisibility(View.GONE);
               patrolRecord_bottom_layout.setVisibility(View.GONE);
               patrolRecord_hzqp_btn.setVisibility(View.VISIBLE);
               patrolRecordBottomTitle.setVisibility(View.GONE);
               patrolRecordSave.setVisibility(View.GONE);
               patrolRecordSend.setVisibility(View.GONE);
               patrolRecord_bottomqp_layout.setVisibility(View.VISIBLE);
               backRightIcon.setVisibility(View.VISIBLE);
               backRightIcon.setText("编辑");
               backRightIcon.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       Intent intent=new Intent(LookPatrolRecordActivity.this,EditPatrolRecordActivity.class);
                       intent.putExtra(AppConstants.EXTRA_XCJLBID,dataBean.getXcjlbid());
                       intent.putExtra("type","hzEdit");//河长编辑
                       startActivity(intent);
                   }
               });
               patrolRecord_hzqp_btn.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   //跳转到签字界面
                   Intent intent=new Intent(LookPatrolRecordActivity.this,PaintActivity.class);
                   intent.putExtra("gpsid",String.valueOf(mPresenter.getConfigBean().getGpsId()));
                   intent.putExtra("gpsnm",mPresenter.getConfigBean().getGpsName());
                   intent.putExtra("xcjlbid",mdataBean.getXcjlbid());
                   startActivityForResult(intent,100);
               }
           });

               break;

       }
    }

    @Override
    public void submittedXcjlbSuccess(XcjlbInfoBean.DataBean dataBean, String type) {
        if (type.equals("neatenXcjlb"))
        {
            //归档
            showMessage("发送成功");
            killMyself();
        }else {
            //发送给河长签批
            showMessage("报送成功");
            Intent intent=new Intent();
            setResult(RESULT_OK,intent);
            killMyself();
        }
    }



    public enum Type implements Serializable {
        LOOK, BSHZQP, EDIT, FSHZBCD,HZQP
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_look_patrol_record; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        loadingDailog = new LoadingDailog.Builder(this).setMessage("处理中...").create();
        setlIstener();
    }

    public void setlIstener() {
        patrolRecordSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        patrolRecordSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    @Override
    public void showLoading() {
        loadingDailog.show();
    }

    @Override
    public void hideLoading() {
        loadingDailog.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.makeText(this, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    /**
     * @param context
     * @param mode    必须
     * @param title
     */
    public static void startLookPatrolRecordActivity_new(Context context, String xcjlbid, Type mode, String title) {
        Intent intent = new Intent(context, LookPatrolRecordActivity.class);
        intent.putExtra(AppConstants.EXTRA_XCJLBID, xcjlbid);
        intent.putExtra(AppConstants.EXTRA_INT, mode);
        intent.putExtra(AppConstants.EXTRA_STRING, title);
//        context.startActivity(intent);
        ((Activity) context).startActivityForResult(intent, 1001);
    }

    /**
     * @param context
     * @param mode    必须
     * @param title
     */
    public static void startLookPatrolRecordActivity(Context context, String xcjlbid, Type mode, String title) {
        Intent intent = new Intent(context, LookPatrolRecordActivity.class);
        intent.putExtra(AppConstants.EXTRA_XCJLBID, xcjlbid);
        intent.putExtra(AppConstants.EXTRA_INT, mode);
        intent.putExtra(AppConstants.EXTRA_STRING, title);
        context.startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 100 && resultCode == Activity.RESULT_OK) {
            String savePath = data.getStringExtra(PenConfig.SAVE_PATH);
            mPresenter.uploadFile(savePath);
        }
    }

    @Override
    public void signSuccess() {
        showMessage("签批成功");
        finish();
    }

    @Override
    public Map<String, String> getSignParams() {
        Map<String,String> params=new HashMap<>();
        params.put("gpsid",String.valueOf(mPresenter.getConfigBean().getGpsId()));
        params.put("gpsnm",mPresenter.getConfigBean().getGpsName());
        params.put("xcjlbid",mdataBean.getXcjlbid());
        return params;
    }
}
